<template>
	<view class="content fafafa">
		<view class="padding">
			<view class="flexbetween margin_bottom3 padding">
				<view class="text_g color_ling margin_right3">新手机号</view>
				<input class="flex1 textright text_a" type="text" placeholder="请输入新手机号"
					placeholder-style="font-size: 30rpx;font-weight: 500;color: #999999;" v-model="mobile">
			</view>
			<view class="flexbetween margin_bottom3 padding">
				<view class="text_g color_ling margin_right3" style="min-width: 100rpx;">验证码</view>
				<view class="flexleft">
					<input class="textright text_a" type="text" placeholder="请输入验证码"
						placeholder-style="font-size: 30rpx;font-weight: 500;color: #999999;" v-model="captcha">
					<view class="line margin_left2 margin_right2"></view>
					<view class="huoqu" @click="sendcode">{{codetext}}</view>
				</view>
			</view>
			<!-- 底部 -->
			<view class="footer ffffff padding">
				<view class="footerButton" @click="checkCaptcha">确认更换</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				mobile:"",
				captcha:"",
				codetext: "获取验证码",
			}
		},
		methods: {
			// 发送验证码
			sendcode() {
				let _this = this
				let mobile = _this.mobile;
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				httpRequest.request('/api/sms/send', 'POST', {
					mobile: mobile,
					event: "changemobile"
				}).then(resp => {
					if (resp.code == 1) {
						var i = 60;
						var timer = setInterval(function() {
							_this.codetext = i + "秒";
							if (i < 1) {
								clearInterval(timer);
								_this.codetext = "获取验证码";
							} else {
								i--;
							}
						}, 1000)
			
					} else if (resp.code == 0) {
						httpRequest.toast(resp.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				})
			},
			// 检测验证码
			checkCaptcha() {
				let mobile = this.mobile;
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				if (this.captcha=="") {
					httpRequest.toast("请输入验证码");
					return false;
				}
				httpRequest.request('/api/sms/check', 'POST', {
					mobile: mobile,
					event: "changemobile",
					captcha: this.captcha
				}).then(resp => {
					if (resp.code == 1) {
						this.submit()
					} else {
						httpRequest.toast(resp.msg);
					}
				})
			},
			submit(){
				if (!this.mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				if (this.captcha=="") {
					httpRequest.toast("请输入验证码");
					return false;
				}
				httpRequest.request('/api/user/changemobile', 'POST', {
					mobile: this.mobile,
					captcha: this.captcha
				}).then(res => {
					httpRequest.toast(res.msg);
					if(res.code==1){
						httpRequest.toast("修改成功");
						uni.removeStorageSync('token');
						uni.removeStorageSync('user_id');
						setTimeout(() => {
							uni.switchTab({
								url:"/pages/mine/mine"
							})
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.line {
		width: 0;
		height: 20rpx;
		border-right: 1rpx solid #999999;
	}

	.huoqu {
		font-size: 30rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #F3271C;
		min-width: 150rpx;
		text-align: right;
	}
</style>